<?php
namespace app\admin\controller;
use app\admin\logic\MistakeLogic;

/**
 * 知错就改api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2020/18/01
 * Time: 18:01
 */
class Mistake extends Base {

    public function __construct(){
        parent::__construct();
        $this->m_logic = new MistakeLogic();
    }

    /**
     * 获取巡店
     */
    public function ajax_get_xd(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->m_logic->get_search_xd_maps($params['department'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];
        $list = model('mistake')->get_list($maps,'id,content,now_date','now_date desc','',$limit,$params['rows']);
     
        $total_num = model('mistake')->get_count($maps,'id');
        
        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->m_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取事项
     */
    public function ajax_get_sx(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->m_logic->get_search_sx_maps($params['username'],$params['department'],$params['start_time'],$params['end_time'],$params['flag']);
        $limit = ($params['page'] - 1) * $params['rows'];
        $list = model('mistake')->get_list($maps,'id,openid,content,flag,now_date','now_date desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username,department');
            $list[$k]['username'] = !empty($u_detail) ? $u_detail['username'] : '';
            $list[$k]['department'] = !empty($u_detail) ? $u_detail['department'] : '';

            $f_arr = explode('_',$v['flag']);
            $m_maps = [];
            $m_maps[] = ['sourceid','=',$f_arr[1]];
            $m_maps[] = ['source','=',$f_arr[2]];
            $m_detail = model('matter')->get_detail($m_maps,'id');
            
            $list[$k]['matterid'] = !empty($m_detail) ? $m_detail['id'] : 0;
        }

        $total_num = model('mistake')->get_count($maps,'id');
        
        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->m_logic->get_response(1,'OK',$data));
    }
}